<?php
declare (strict_types=1);

namespace app\service;
use app\common\model\ArticlePv;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/7
 * Time: 11:51
 * qq 571031767
 */

class ContentService
{
    //每日热门
    public static function hot_day($params=[])
    {
        if(isset($params["limit"])){
            $limit = $params["limit"];
        }else{
            $limit = 10;
        }

        if(isset($params["type"])){
            $date = "-7 days";
        }else{
            $date = "today";
        }



        $data = ArticlePv::with("content")
            ->whereTime('create_time', $date)
            ->field("id,create_time,cid,COUNT(cid) AS count_cid")
            ->group("cid")
            ->order("count_cid DESC")
            ->limit($limit)
            ->select();

        foreach ($data as $k => $v){
            $data[$k]["prefimg"] = handlePic($v["content"]["prefimg"]);
            $data[$k]["image_url"] =[$data[$k]["prefimg"]];
            $data[$k]["article_type"] =2;
            if($v["content"]["comment_count"]>0){
                $data[$k]["comment_count"] = $v["content"]["comment_count"];
            }else{
                $data[$k]["comment_count"] = 0;
            }
            if(!$v["content"]["author"] || $v["content"]["author"] == 'null'){
                $data[$k]["author"] = "米醋儿";
            }
            $data[$k]["source"] = $data[$k]["author"];
            $data[$k]["title"] = $v["content"]["title"];
            $data[$k]["id"] = $v["content"]["id"];

            //更新文章的展现量
            update_hits($v["content"]["id"]);

            //热度计算
            $data[$k]["hot"] = $v["count_cid"]*30 + 14400;
        }

        return $data;
    }

    //保存视频
    public static function save_video($params=[])
    {

        $user = $params["user"];
        $d = $params;
        $mod = new \app\common\model\Content();
        if(!isset($d["status"])){
            $d["status"] = 1;
        }
        //超级管理员选择了发布文章的用户
        if (isset($d["post_uid"]) && $d["post_uid"] != ''){
            $user = User::find($d["post_uid"]);
        }
        $d["uid"] = $user["id"];
        $d["author"] = $user["nickname"];
        $d["userinfo"] = json_encode($user);
        if(!isset($d["model_id"])){
            $d["model_id"] = 1;
        }
        //修改
        if(isset($d["id"])){
            $mod = Content::find($d["id"]);
        }
        if(!isset($d["cate_id"])){
            $d["cate_id"] = 0;
        }
        foreach ($d as $k => $v){
            if($v == null || !$v || $v=='null'){
                unset($d[$k]);
            }
        }
        if(isset($d["table-align"])){
            unset($d["table-align"]);
        }
        $res = $mod->save($d);

        //如果是文章模型，则需要将文章存进去
        if($d["model_id"] == 2){
            $d["contentid"] = $mod->id;
            if(isset($d["id"])){
                \app\common\model\Video::where("contentid",$d["contentid"])
                    ->save($d);
            }else{
                \app\common\model\Video::create($d);
            }
        }
        if($res){
            return  $mod->id;
        }else{
            return  false;
        }
    }
}


